package com.lanxiao.cloud.modules.oauth.controller;

import com.lanxiao.cloud.common.core.dto.PageQuery;
import com.lanxiao.cloud.common.core.dto.ResultModel;
import com.lanxiao.cloud.common.core.enums.OperationType;
import com.lanxiao.cloud.common.web.invalid.Log;
import com.lanxiao.cloud.modules.oauth.application.OauthCmdManager;
import com.lanxiao.cloud.modules.oauth.application.OauthQueryManager;
import com.lanxiao.cloud.modules.oauth.application.dto.LoginUserInfoDto;
import com.lanxiao.cloud.modules.oauth.application.dto.UserAuthPwdCmdUpdate;
import com.lanxiao.cloud.modules.oauth.application.dto.UserDetailsDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：qiaoliang
 * @date ：2021-09-10
 */
@Api(tags = "认证")
@Validated
@RequestMapping("/oauths")
@RestController
public class OauthController {

  private final OauthQueryManager oauthQueryManager;
  private final OauthCmdManager oauthCmdManager;

  public OauthController(
      OauthQueryManager oauthQueryManager,
      OauthCmdManager oauthCmdManager) {
    this.oauthQueryManager = oauthQueryManager;
    this.oauthCmdManager = oauthCmdManager;
  }

  @ApiOperation(value = "条件查询在线用户列表")
  @GetMapping("/on-line")
  public ResultModel<UserDetailsDto> onLineUser(@RequestParam(required = false) Long id, PageQuery pageQuery) {
    return oauthQueryManager.onLineUser(id, pageQuery);
  }


  @ApiOperation(value = "查询当前用户信息")
  @GetMapping("/info")
  public LoginUserInfoDto onLineUser() {
    return oauthQueryManager.loginUserInfoDto();
  }


  @ApiOperation(value = "修改个人账号密码")
  @Log(operationType = OperationType.UPDATE)
  @PutMapping("/pwd")
  public ResponseEntity<String> updatePwd(@Valid @RequestBody UserAuthPwdCmdUpdate update) {
    oauthCmdManager.updatePwd(update);
    return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).build();
  }

}
